<?php
// +----------------------------------------------------------------------
// | WoShiMaiJia Projcet 
// +----------------------------------------------------------------------
// | Copyright (c) 2010-2011 http://woshimaijia.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: xinqiyang <xinqiyang@gmail.com>
// +----------------------------------------------------------------------

class TopicLogic extends BaseService
{
	/**
	 * adds topic
	 * @param array $array
	 */
	public static function addTopic($array)
	{
		//add topic
		self::add('post', self::topicPost($array));
		$array['desc'] = $array['content'];
		unset($array['content']);
		return self::add('topic', $array);
	}
	
	public static function topicPost($array)
	{
		$data['id']=objid();
		$data['content']=$array['content'];
		$data['istopic']='1';
		$data['createtime']=time();
		$data['status']='0';
		$data['topic_id']=$array['id'];
		$data['user_id']=$array['user_id'];
		$data['object']='topic';
		$data['istop']='0';
		return $data;
	}
	
	/**
	 * get topic
	 * @param bigint $group_id
	 * @param bigint $user_id
	 * @param int $offset
	 * @param int $num
	 */
	public static function getsTopic($group_id='',$user_id='',$offset=0,$num=40)
	{
		if($group_id)
		{
			$where ="group_id={$group_id} and status=0 order by id desc limit {$offset},{$num}";
		}elseif($user_id){
			$where = "user_id={$user_id} and status=0 order by id desc limit {$offset},{$num}";
		}else{
			$where = "status=0 order by id desc limit {$offset},{$num}";
		}
		return self::gets('topic', $array);
	}
	
	
	public static function getsMyJoinGroupTopics($groupids,$offset=0,$num=40)
	{
		$strin = strin($groupids);
		$where = empty($strin) ? "status=0 ORDER BY id desc" : "status=0 and group_id in ".$strin."  ORDER BY id desc";
		return self::getsByPage('topic', $where,$offset,$num);
	}
	
	
	public static function destoryTopic($array)
	{
		return self::destory('topic', $array);
	}
	
	/**
	 * set top topic
	 * @param array $array id , istop (order by istop desc)
	 */
	public static function setTop($array)
	{
		$r = self::save('topic', $array);
		if($r){
			//@todo del cache then return
		}
		return $r;
	}
	
	
	public static function forbidTopic($array,$type=1)
	{
		return self::save('topic', array('islock'=>$type),'id in .'.strin($array));
	}
	
}